package com.rattkin.controller.extension
{
	import com.asfusion.mate.actionLists.IScope;
	import com.asfusion.mate.actions.IAction;
	import com.asfusion.mate.actions.StopHandlers;
	import com.asfusion.mate.utils.debug.LogInfo;
	import com.asfusion.mate.utils.debug.LogTypes;
	
	import mx.logging.ILogger;
		
	public class StopHandlers extends com.asfusion.mate.actions.StopHandlers implements IAction
	{
		private var _stopHandlers:Array;  
		
		public function StopHandlers()  {  
			super();  
		}  

		public function get stopHandlers():Array {
			return _stopHandlers;
		}
		
		[ArrayElementType("com.asfusion.mate.actions.IAction")]
		public function set stopHandlers(value:Array):void {
			_stopHandlers = value;
		}
		
		override protected function run(scope:IScope):void {  
			super.run(scope); 

            if( lastReturnEquals == null && scope.lastReturn == null )  {  
                 scope.stopRunning();  
            }  			
            
            if (!scope.isRunning()) {
            	if (stopHandlers && stopHandlers.length) {
					runSequence( scope, stopHandlers );
				}
            }
		} 

		protected function runSequence(scope:IScope, actionList:Array):void	{
			var logger:ILogger = scope.getLogger();
			logger.info(LogTypes.SEQUENCE_START, new LogInfo(scope, null));
				
			for each (var action:IAction in actionList)
			{
				scope.currentTarget = action;
				logger.info( LogTypes.SEQUENCE_TRIGGER, new LogInfo(scope, null));
				action.trigger(scope);
			}
			
			logger.info( LogTypes.SEQUENCE_END, new LogInfo( scope, null));
		}		
		 
	}
}